#include "common.h"

/*** Client for echo-server ***/

int main() {
    int sockfd;
    struct sockaddr_in addr;
    char buf[BUF_SIZE];
    char message[BUF_SIZE];
    
    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        perror("Error in socket creation");
        return errno;
    }

    bzero(&addr, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(MY_PORT);
    addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
        
    if (connect(sockfd, (struct sockaddr*) &addr, sizeof(addr)) < 0) {
        perror("Error in connection");
        close(sockfd);
        return errno;
    }
    
    sprintf(message, "Message from pid: %d", getpid());
    sendto(sockfd, message, sizeof(message), 0, (struct sockaddr*) &addr, sizeof(addr));
    int n = recvfrom(sockfd, buf, sizeof(message), 0, 0, 0);
    buf[n] = '\0';
    printf("Received: %s\n", buf);
    close(sockfd);
    return 0;
}
